<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    xmlns:Utils="clr-namespace:Patterns4Net.ArchitectureExplorer.Views.Utils;assembly=aexplorer"
    xmlns:p4n="urn:p4n"
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
    exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes" cdata-section-elements="String"/>

  <msxsl:script implements-prefix="p4n" language="C#">
    <msxsl:assembly name="System.Core"/>
    <msxsl:using namespace="System.Linq"/>
    <msxsl:using namespace="System.Text"/>
    <![CDATA[
    public string SubstringAfterLast(string content, string needle)
    {
      int idx = content.LastIndexOf(needle);
      if (idx < 0) { return content; }
      
      return content.Substring(idx + 1);
    }    
    ]]>
  </msxsl:script>

  <xsl:template match="/">
    <TextBlock>
      <TextBlock.Resources>
        <Style TargetType="TextBlock">
          <Setter Property="Margin" Value="0, 0, 0, 10"/>
          <Setter Property="TextWrapping" Value="Wrap"/>
        </Style>
      </TextBlock.Resources>
      <xsl:apply-templates/>
    </TextBlock>
  </xsl:template>

  <xsl:template match="content">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="remarks">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="sources">
    <TextBlock Padding="3, 0, 0, 0">
      <Span FontSize="13">
        Sources:
      </Span>
      <LineBreak/>
      <TextBlock Padding="7, 0, 0, 0">
      <xsl:for-each select="source">
        <Span>
          <xsl:value-of select="."/>
        </Span>
        <LineBreak/>
      </xsl:for-each>
      </TextBlock>
    </TextBlock>
  </xsl:template>
  
  <xsl:template match="summary">
    <xsl:choose>
      <xsl:when test="count(content/para)>0">
         <xsl:call-template name="content"/>
      </xsl:when>
      <xsl:otherwise>
        <!-- if the summary does not contain paragraph, we create one -->
        <TextBlock>
          <xsl:call-template name="content"/>
        </TextBlock>        
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="para">
    <TextBlock>
      <xsl:call-template name="content"/>
    </TextBlock>
  </xsl:template>

  <xsl:template name="content">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="c">
    <Bold>
      <xsl:value-of select="text()"/>
    </Bold>
  </xsl:template>

  <xsl:template match="see|seealso">
    <Span FontStyle="Italic">
      <xsl:value-of select='p4n:SubstringAfterLast(string(@cref), ".")'/>
    </Span>
  </xsl:template>

  <xsl:template match="example|value">
    <xsl:apply-templates/>
  </xsl:template>
  
  <xsl:template match="typeparamref">
    <Bold>
      <xsl:value-of select="@name"/>
    </Bold>
  </xsl:template>

  <xsl:template match="code">
    <Utils:CodeTextBlock Background="#EEEEEE" FontFamily="Consolas" xml:space="preserve">
      <Utils:CodeTextBlock.Code>
        <system:String xml:space="preserve">
           <xsl:value-of select="string(text())" disable-output-escaping="yes"/>
        </system:String>
      </Utils:CodeTextBlock.Code>
    </Utils:CodeTextBlock>
  </xsl:template>  

  <xsl:template match="list">
    <TextBlock Margin="0,0,0,0" Padding="3, 0, 0, 0">
      
      <xsl:if test="listheader">
        <Span FontSize="14">
          <xsl:value-of select="term"/>
        </Span>        
        <Span FontStyle="Italic">
          <xsl:text><![CDATA[ ]]> </xsl:text><xsl:value-of select="description"/>
        </Span>
        <LineBreak/>
      </xsl:if>
      
      <TextBlock Padding="7, 0, 0, 0">
        <xsl:for-each select="(item)">
          <Span FontSize="13">- <xsl:value-of select="term"/></Span>
          <xsl:if test="description">
            <Span FontStyle="Italic">
              <xsl:text><![CDATA[ ]]> </xsl:text><xsl:value-of select="description"/>
            </Span>
          </xsl:if>
          <LineBreak/>
        </xsl:for-each>
      </TextBlock>
    </TextBlock>
  </xsl:template>
</xsl:stylesheet>
